package com.gitee.controller;

import com.gitee.bo.CartAddBo;
import com.gitee.bo.CartUpdateBo;
import com.gitee.enums.ResponseEnum;
import com.gitee.service.ICartService;
import com.gitee.utils.JwtUtil;
import com.gitee.vo.CartVo;
import com.gitee.vo.ResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private ICartService cartService;

    @GetMapping
    public ResponseVo<CartVo> getCart(HttpServletRequest request, Integer cookbookType) {
        Integer userId = JwtUtil.getUserId(request.getHeader("token"));
        CartVo cartVo = cartService.getCart(userId, cookbookType);
        return ResponseVo.ok(cartVo);
    }

    @PostMapping
    public ResponseVo<String> addCart(HttpServletRequest request, @RequestBody CartAddBo cartAddBo) {
        Integer userId = JwtUtil.getUserId(request.getHeader("token"));
        cartService.addCart(userId, cartAddBo);
        return ResponseVo.ok(ResponseEnum.ADD_SUCCESS);
    }

    @PutMapping
    public ResponseVo<CartVo> updateCart(HttpServletRequest request, @RequestBody CartUpdateBo cartUpdateBo,
                                         Integer foodId, Integer cookbookId, Integer cookbookType) {
        Integer userId = JwtUtil.getUserId(request.getHeader("token"));
        CartVo cartVo = cartService.updateCart(userId, foodId, cookbookId, cookbookType, cartUpdateBo);
        return ResponseVo.ok(cartVo);
    }

    @DeleteMapping
    public ResponseVo<CartVo> deleteCart(HttpServletRequest request, Integer foodId, Integer cookbookId, Integer cookbookType) {
        Integer userId = JwtUtil.getUserId(request.getHeader("token"));
        CartVo cartVo = cartService.removeCart(userId, foodId, cookbookId, cookbookType);
        return ResponseVo.ok(cartVo);
    }

    @GetMapping("/settle")
    public ResponseVo<CartVo> settleCart(HttpServletRequest request, Integer cookbookType) {
        Integer userId = JwtUtil.getUserId(request.getHeader("token"));
        CartVo cartVo = cartService.settleCart(userId, cookbookType);
        return ResponseVo.ok(cartVo);
    }

    @PostMapping("/selectAll")
    public ResponseVo<CartVo> selectAll(HttpServletRequest request, Integer cookbookId, Integer cookbookType) {
        Integer userId = JwtUtil.getUserId(request.getHeader("token"));
        CartVo cartVo = cartService.selectAll(userId, cookbookId, cookbookType);
        return ResponseVo.ok(cartVo);
    }

    @PostMapping("/unSelectAll")
    public ResponseVo<CartVo> unSelectAll(HttpServletRequest request, Integer cookbookId, Integer cookbookType) {
        Integer userId = JwtUtil.getUserId(request.getHeader("token"));
        CartVo cartVo = cartService.unSelectAll(userId, cookbookId, cookbookType);
        return ResponseVo.ok(cartVo);
    }

}
